package com.tdam2014.grupo3;

import java.util.ArrayList;

import android.app.ListFragment;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.tdam2014.grupo3.adapters.CommentAdapter;
import com.tdam2014.grupo3.db.CommentDatabaseHelper;
import com.tdam2014.grupo3.entities.Comment;
import com.tdam2014.grupo3.interfaces.UpdateableUI;
import com.tdam2014.grupo3.tasks.UpdateCommentsListTask;

public class CommentFragment extends ListFragment implements UpdateableUI {
	
	private Menu commentMenu;
	private String photoID;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		setHasOptionsMenu(true);
		
		View rootView = inflater.inflate(R.layout.fragment_comment, container, false);
		return rootView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		// Obtengo el ID de la foto y consulto los comentarios que posee la misma
		photoID = getActivity().getIntent().getStringExtra(PhotoActivity.KEY_PHOTO_ID);
		ArrayList<Comment> comments = null;
		if(photoID != null) comments = new CommentDatabaseHelper(getActivity().getApplicationContext()).getCommentList(photoID);
		
		// Por ultimo creo el adapter de los comentarios, y le seteo la lista al adapter y el adapter al fragment
		final CommentAdapter commentAdapter = new CommentAdapter(getActivity().getApplicationContext());
		commentAdapter.setComments(comments);
		setListAdapter(commentAdapter);
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.update_options, menu);
		this.commentMenu = menu;
		
		if(getListAdapter().getCount() == 0) updateComments();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
		        NavUtils.navigateUpFromSameTask(getActivity());
		        return true;
			case R.id.update:
				updateComments();
				break;
		}
		
		return true;
	}
	
	@Override
	public CommentAdapter getListAdapter() {
		return (CommentAdapter) super.getListAdapter();
	}
	
	private void updateComments() {
		if(photoID != null) {
			UpdateCommentsListTask updateCommentsListTask = new UpdateCommentsListTask(getActivity(), photoID, this);
			updateCommentsListTask.execute(photoID);
		}
	}

	@Override
	public void onStartUpdate() {
		((TextView) getListView().getEmptyView()).setText(getResources().getString(R.string.updating) + " " + getResources().getString(R.string.comments) + "...");
		
		commentMenu.findItem(R.id.update).setVisible(false);
		getActivity().setProgressBarIndeterminateVisibility(true);
		
		ArrayList<Comment> comments = new ArrayList<Comment>();
		getListAdapter().setComments(comments);
		getListAdapter().notifyDataSetChanged();
	}

	@Override
	public void onFinishUpdate() {
		((TextView) getListView().getEmptyView()).setText(getResources().getString(R.string.empty_list_comments));
		
		ArrayList<Comment> comments = new CommentDatabaseHelper(getActivity().getApplicationContext()).getCommentList(photoID);
		getListAdapter().setComments(comments);
		getListAdapter().notifyDataSetChanged();
		
		getActivity().setProgressBarIndeterminateVisibility(false);
		commentMenu.findItem(R.id.update).setVisible(true);
	}

	@Override
	public void informError(String error) {
		((TextView) getListView().getEmptyView()).setText(error);
		
		ArrayList<Comment> comments = new CommentDatabaseHelper(getActivity().getApplicationContext()).getCommentList(photoID);
		getListAdapter().setComments(comments);
		getListAdapter().notifyDataSetChanged();
		
		getActivity().setProgressBarIndeterminateVisibility(false);
		commentMenu.findItem(R.id.update).setVisible(true);
		
		if(!comments.isEmpty()) Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
	}
}